package com.hafu.view.kehu;

import com.hafu.model.KhInfo;
import com.hafu.service.KhInfoService;
import com.hafu.util.StringUtil;
import java.awt.Component;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.util.ArrayList;
import javax.swing.GroupLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.GroupLayout.Alignment;

    public class AddKeHuFrm extends JFrame {
        private KhInfoService khInfoService = new KhInfoService();


        public AddKeHuFrm() {
            this.initComponents();
        }

        private void button1(ActionEvent e) {
            String name = this.textField1.getText();
            String phone = this.textField2.getText();
            String area = this.textField3.getText();
            if (StringUtil.isEmpty(name)) {
                JOptionPane.showMessageDialog((Component)null, "学生姓名不能为空");
            } else if (StringUtil.isEmpty(phone)) {
                JOptionPane.showMessageDialog((Component)null, "学生电话不能为空");
            } else if (!StringUtil.isPhoneNumber(phone)) {
                JOptionPane.showMessageDialog((Component)null, "电话号码格式不正确");
            } else if (StringUtil.isEmpty(area)) {
                JOptionPane.showMessageDialog((Component)null, "学生地址不能为空");
            } else {
                KhInfo khInfo = new KhInfo(name, phone,area);
                ArrayList<KhInfo> khInfoByName = this.khInfoService.getKhInfoByName(khInfo);
                if (khInfoByName.size() > 0) {
                    JOptionPane.showMessageDialog((Component)null, "学生信息已存在,请勿重复添加");
                } else {
                    Boolean aBoolean = this.khInfoService.addKhInfo(khInfo);
                    if (aBoolean) {
                        JOptionPane.showMessageDialog((Component)null, "添加成功");
                        this.dispose();
                    } else {
                        JOptionPane.showMessageDialog((Component)null, "添加失败");
                    }

                }
            }
        }

    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents  @formatter:off
        // Generated using JFormDesigner Evaluation license - aaaa
        label1 = new JLabel();
        label2 = new JLabel();
        textField1 = new JTextField();
        textField2 = new JTextField();
        button1 = new JButton();
        label3 = new JLabel();
        textField3 = new JTextField();

        //======== this ========
        setTitle("\u6dfb\u52a0\u5b66\u751f\u4fe1\u606f");
        var contentPane = getContentPane();

        //---- label1 ----
        label1.setText("\u5b66\u751f\u540d\u5b57");

        //---- label2 ----
        label2.setText("\u5b66\u751f\u7535\u8bdd");

        //---- button1 ----
        button1.setText("\u63d0\u4ea4");
        button1.addActionListener(e -> button1(e));

        //---- label3 ----
        label3.setText("\u5b66\u751f\u5730\u5740");

        GroupLayout contentPaneLayout = new GroupLayout(contentPane);
        contentPane.setLayout(contentPaneLayout);
        contentPaneLayout.setHorizontalGroup(
            contentPaneLayout.createParallelGroup()
                .addGroup(contentPaneLayout.createSequentialGroup()
                    .addGroup(contentPaneLayout.createParallelGroup()
                        .addGroup(contentPaneLayout.createSequentialGroup()
                            .addGap(147, 147, 147)
                            .addGroup(contentPaneLayout.createParallelGroup()
                                .addComponent(label1)
                                .addComponent(label3, GroupLayout.PREFERRED_SIZE, 62, GroupLayout.PREFERRED_SIZE)
                                .addComponent(label2, GroupLayout.PREFERRED_SIZE, 62, GroupLayout.PREFERRED_SIZE))
                            .addGap(86, 86, 86)
                            .addGroup(contentPaneLayout.createParallelGroup(GroupLayout.Alignment.LEADING, false)
                                .addComponent(textField1)
                                .addComponent(textField3, GroupLayout.PREFERRED_SIZE, 198, GroupLayout.PREFERRED_SIZE)
                                .addComponent(textField2, GroupLayout.Alignment.TRAILING)))
                        .addGroup(contentPaneLayout.createSequentialGroup()
                            .addGap(207, 207, 207)
                            .addComponent(button1, GroupLayout.PREFERRED_SIZE, 269, GroupLayout.PREFERRED_SIZE)))
                    .addContainerGap(265, Short.MAX_VALUE))
        );
        contentPaneLayout.setVerticalGroup(
            contentPaneLayout.createParallelGroup()
                .addGroup(contentPaneLayout.createSequentialGroup()
                    .addGap(106, 106, 106)
                    .addGroup(contentPaneLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                        .addComponent(label1)
                        .addComponent(textField1, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
                    .addGap(17, 17, 17)
                    .addGroup(contentPaneLayout.createParallelGroup(GroupLayout.Alignment.LEADING, false)
                        .addComponent(textField2)
                        .addComponent(label2, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addGap(18, 18, 18)
                    .addGroup(contentPaneLayout.createParallelGroup()
                        .addComponent(label3, GroupLayout.PREFERRED_SIZE, 25, GroupLayout.PREFERRED_SIZE)
                        .addComponent(textField3, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
                    .addGap(53, 53, 53)
                    .addComponent(button1)
                    .addContainerGap(124, Short.MAX_VALUE))
        );
        pack();
        setLocationRelativeTo(getOwner());
        // JFormDesigner - End of component initialization  //GEN-END:initComponents  @formatter:on
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables  @formatter:off
    // Generated using JFormDesigner Evaluation license - aaaa
    private JLabel label1;
    private JLabel label2;
    private JTextField textField1;
    private JTextField textField2;
    private JButton button1;
    private JLabel label3;
    private JTextField textField3;
    // JFormDesigner - End of variables declaration  //GEN-END:variables  @formatter:on
}
